##########################################################
# John Henderson and Alex Theodoridis
# Replication Data for: "Seeing Spots", 
#  Forthcoming in Political Behavior, August 20, 2017
# 
##########################################################
#
#  figure_1.R
#  -- file produces the density plots Figure 1
#
##########################################################

rm(list=ls())
source('~/Dropbox/Seeing_Spots/replication/pre_data.R')
                 
newsint_old = newsint - 2 
newsint_alt = rowSums(cbind(sen_majority_correct,hou_majority_correct))-1 
#newsint[which(newsint<0)] = -1
#newsint[which(newsint>0)] =  1

college=as.numeric(cces_data$educ)
college[which(as.numeric(cces_data$educ)<3)]=-1
college[which(as.numeric(cces_data$educ)==3 | as.numeric(cces_data$educ)==4)]=0
college[which(as.numeric(cces_data$educ)>4)]=1

newsint_edu = college

# density plots - interesteds

completes = !is.na(time_watched) & !is.na(pid_lean) & !is.na(newsint_old)

ytime 			= time_watched[completes] 
tr_pos_ad 		= pos_ad[completes] 
tr_neg_ad    	= neg_ad[completes] 
tr_obama_ad   	= obama_ad[completes] 
tr_romney_ad  	= romney_ad[completes] 
tr_obama_pos	= pos_ad[completes] 
tr_obama_neg  	= neg_ad[completes] 
tr_romney_pos 	= obama_ad[completes] 
tr_romney_neg 	= romney_ad[completes] 
pid				= pid_lean[completes] 
newsint_alt		= newsint_alt[completes] 
newsint_old		= newsint_old[completes] 
newsint_edu		= newsint_edu[completes] 

#lows = newsint_old==-1
#meds = newsint_old== 0
#high = newsint_old== 1       

lows = newsint_alt==-1
meds = newsint_alt== 0
high = newsint_alt== 1
                     
dems = pid==-1
inds = pid== 0
reps = pid== 1    
          

# pos v. neg
{                                        
pdf(file='~/Dropbox/Seeing_Spots/replication/figures/density_time_by_tone_both.pdf')
plot(density(ytime[tr_pos_ad],from=0,to=55),main='',xlab='Time Watched',ylim=c(0,.09),col='black',lwd=2)  
lines(density(ytime[tr_neg_ad],from=0,to=55),main='',xlab='Time Watched',lty=2,col='black',lwd=2)      
legend(cex=1.45,x=c(1),y=c(.08),legend=c('Positive Ad'),lty=c(1),box.col="#0000ff00",col='black',lwd=2)  
legend(cex=1.45,x=c(1),y=c(.075),legend=c('Negative Ad'),lty=c(2),box.col="#0000ff00",col='black',lwd=2)
ksp=round(ks.test(c(ytime[tr_pos_ad]),ytime[c(tr_neg_ad)])$p.value,digits=3)
tp=round(t.test(c(ytime[tr_pos_ad]),ytime[c(tr_neg_ad)])$p.value,digits=3)  

text(cex=1.45,x=c(47),y=c(.085),label=c(expression(italic(paste(p,' = ',0.583),sep=''))),
  col='black',lwd=2)
dev.off()
}


# END figure_1.R